package com.sdy.healthcode.web.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.StringUtil;
import com.sdy.healthcode.biz.model.ExpertsInformation;
import com.sdy.healthcode.biz.model.ReportPeoplePass;
import com.sdy.healthcode.biz.param.DelDeptByIdParam;
import com.sdy.healthcode.biz.param.UpdateDeptOrAddDeptParam;
import com.sdy.healthcode.biz.service.ExpertsInformationService;
import com.sdy.healthcode.biz.service.HsDepartmentService;
import com.sdy.healthcode.biz.model.HsDepartment;
import com.sdy.common.model.Response;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sdy.mvc.controller.BaseController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 科室表 前端控制器
 * </p>
 *
 * @author hyh
 * @since 2020-03-09
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@Api(tags = "部门科室Api")
@RequestMapping("/hsDepartment")
public class HsDepartmentController extends BaseController {
    @Autowired
    private HsDepartmentService hsDepartmentService;
    @Autowired
    private ExpertsInformationService expertsInformationService;

    @ApiOperation(value = "获取部门分页列表", tags = "", response = HsDepartment.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "根据部门名称模糊查询")
    })
    @GetMapping("/getDeptPageList")
    public Response getDeptPageList(String name, Page page) throws BizException, UnsupportedEncodingException {
        name = URLDecoder.decode(name, StandardCharsets.UTF_8.name());
        IPage<HsDepartment> pageList = hsDepartmentService.getDeptPageList(name, page);
        return Response.success(pageList);
    }

    @ApiOperation(value = "获取部门树状列表", tags = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "根据部门名称模糊查询")
    })
    @GetMapping("/getDeptTreeList")
    public Response getDeptTreeList(String name) throws BizException {
        List<HsDepartment> deptTreeList = hsDepartmentService.getDeptTreeList(name);
        return Response.success(deptTreeList);
    }

    @ApiOperation(value = "修改部门基本信息/新增部门")
    @PostMapping("/updateDeptOrAddDept")
    public Response updateDeptOrAddDept(@ApiParam(name = "param", value = "修改部门基本信息的参数") @RequestBody UpdateDeptOrAddDeptParam param) throws BizException {
        hsDepartmentService.updateOrAddDept(param);
        return Response.success();
    }

    @ApiOperation(value = "根据id删除部门信息", tags = "")
    @PostMapping("/delDeptById")
    public Response delDeptById(@ApiParam(name = "param", value = "修改部门基本信息的参数") @RequestBody DelDeptByIdParam param) throws BizException {
        if (expertsInformationService.lambdaQuery().eq(ExpertsInformation::getDepartmentId, param.getId()).count() > 0) {
            return Response.error("科室下有关联的医生，禁止删除。");
        }
        hsDepartmentService.delDeptById(param);
        return Response.success();
    }

    /**
     * 导出部门科室信息(Excel文件)
     */
    @ApiOperation(value = "导出部门科室信息(Excel文件)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "根据部门名称模糊查询")
    })
    @GetMapping("/downLoadDeptListToExcel")
    public void downLoadDeptListToExcel(String name, HttpServletResponse response) throws IOException {
        name = URLDecoder.decode(name, StandardCharsets.UTF_8.name());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = sdf.format(new Date());
        List<HsDepartment> list = hsDepartmentService.list(Wrappers.<HsDepartment>lambdaQuery()
                .like(StringUtil.isNotBlank(name), HsDepartment::getName, name));
        String fileName = date + "科室信息表.xlsx";
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
        EasyExcel.write(response.getOutputStream(), HsDepartment.class).sheet("科室信息表").doWrite(list);
    }

}
